﻿using PracticeCentre.Common;
using PracticeCentre.DataControl;
using PracticeCentre.Modeles;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PracticeCentre.Business
{
    public class NavigationMenu
    {
        /// <summary>
        /// 获取登录用户的菜单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<Menu> GetLoginUserMenuList(SystemUser model)
        {
            DataControlHelper dataHelper = new DataControlHelper();
            try
            {
                DataTable dt = dataHelper.ExecuteDataTable(ProceduresConfig.GetLoginUserMenu,null);
                DataModelHandler<Menu> dataHandler = new DataModelHandler<Menu>();
                List<Menu> menuList = dataHandler.FillModel(dt);                
                menuList = FillTree(menuList.Where(m=>m.parentId==0).ToList(), menuList);
                return menuList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        private List<Menu> FillTree(List<Menu> parents, List<Menu> menuList)
        {
           List<Menu> childList= menuList.Where(m => m.parentId > 0).ToList();
            foreach (Menu parent in parents)
            {
                parent.children = new List<Itme>();
                foreach (Menu child in childList)
                {
                    if (parent.Id == child.parentId)
                    {                        
                        Itme item = ModelesMapping.Mapping<Itme, Menu>(child);
                        parent.children.Add(item);
                    }
                }
            }
            return parents;
        }
    }
}
